﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section styles{
    <style>
    </style>
}

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="user-form" style="padding: 15px 0 0 0;">
            <input type="hidden" name="Uid" value="@ViewBag.Ids" />
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">授权用户</label>
                <div class="layui-input-block">
                    <div class="layui-btn-container tag">
                        @if (ViewBag.UserList != null)
                        {
                            foreach (var item in ViewBag.UserList)
                            {
                                <button lay-id="@item.Id" type="button" class="tag-item tag-item-normal layui-btn layui-btn-primary layui-btn-sm">@item.UserName - @item.RealName</button>
                            }
                        }
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">授权角色</label>
                <div class="layui-input-block">
                    <div  id="rolelist">
                        @if (ViewBag.RoleList != null)
                        {
                            foreach (var item in ViewBag.RoleList)
                            {

                                <input type="checkbox" name="userrole" lay-skin="primary" value="@item["RoleCode"]" title="@item["RoleName"]" >
                            }
                        }
                    </div>
                </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-hide" lay-submit lay-filter="user-submit" id="user-submit">提交</button>
                    </div>
                </div>
         </form>
    </div>
</div>


@section scripts{
    <script type="text/javascript">
        layui.use(['lscom', 'form'], function () {
            var $ = layui.$;
            var lscom = layui.lscom;
            var form = layui.form;

            var index = parent.layer.getFrameIndex(window.name);
            //parent.layer.iframeAuto(index);

            window.submitForm = function () {
                $("#user-submit").click();
            }
          

            form.on('submit(user-submit)', function (data) {
                let rolecodearry = [];

                $('#rolelist input[name="userrole"]:checked').each(function () {
                    rolecodearry.push($(this).val());
                });
                data.field.Roles = rolecodearry.join(',');

               
                var field = data.field;
               
                var waitIndex = parent.layer.load(2);
                lscom.ajax({
                    url: "/Systems/User/Audit",
                    data: JSON.stringify(field),
                    lsHandleError: false
                }).done(function (data) {
                    if (data.code == 0) {
                        data.index = index;
                        let parentWindow = parent.selectedWindow().window;
                        parentWindow.saveCallback(data);
                    }
                }).fail(function (message) {
                    lscom.notify.error(message);
                }).always(function () {
                    parent.layer.close(waitIndex);
                });

                return false;
            });
        });
    </script>
  
}

